package com.circle.rest.modular.nt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.circle.rest.core.aop.Log;
import com.circle.rest.core.aop.Permission;
import com.circle.rest.core.common.enums.BusinessType;
import com.circle.rest.core.common.enums.NtHistoryStatus;
import com.circle.rest.core.common.model.SuccessResponseData;
import com.circle.rest.core.common.page.PageInfoBT;
import com.circle.rest.core.exception.ServiceException;
import com.circle.rest.core.util.ToolUtil;
import com.circle.rest.modular.nt.model.History;
import com.circle.rest.modular.nt.service.IHistoryService;
import com.circle.rest.modular.nt.wrapper.HistoryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>
 * 通知记录 前端控制器
 * </p>
 *
 * @author 赵航
 * @since 2019-04-09
 */
@Api(value = "通知记录", tags = {"nt : 通知记录API"})
@RestController
@RequestMapping("/nt/history")
public class HistoryController {

    @Autowired
    private IHistoryService historyService;

    /**
     * 查询通知记录列表
     */
    @ApiOperation("查询通知记录列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "index", value = "起始数据下标", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "数据条数", dataType = "int"),
            @ApiImplicitParam(name = "condition", value = "关键字", dataType = "String")
    })
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @Log(title = "查询通知记录列表", businessType = BusinessType.SELECT_LIST)
    @Permission
    public Object list(Integer index, Integer size, String condition) {
        Page<History> page = new Page<>(index == null ? 1 : index, size == null ? 15 : size);
        QueryWrapper<History> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        IPage<Map<String, Object>> mapIPage = historyService.pageMaps(page, wrapper);
        PageInfoBT<Object> objectPageInfoBT = new PageInfoBT<>(new HistoryWrapper(mapIPage).wrap());
        return new SuccessResponseData(objectPageInfoBT);
    }

    /**
     * 添加通知记录
     */
    @ApiOperation("添加通知记录")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @Log(title = "添加通知记录", businessType = BusinessType.INSERT)
    @Permission
    public Object add(History history) {
        if (ToolUtil.isEmpty(history.getContentId())) {
            throw new ServiceException("通知信息编号不能为空");
        }
        history.setUpdateTime(LocalDateTime.now());
        history.setCreateTime(LocalDateTime.now());
        boolean save = historyService.save(history);
        return new SuccessResponseData(save);
    }

    /**
     * 修改通知记录
     */
    @ApiOperation("修改通知记录")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @Log(title = "修改通知记录", businessType = BusinessType.UPDATE)
    @Permission
    public Object edit(History history) {
        if (ToolUtil.isEmpty(history.getId())) {
            throw new ServiceException("编号不能为空");
        }
        if (ToolUtil.isEmpty(history.getVersion())) {
            throw new ServiceException("版本号不能为空");
        }
        History check = historyService.getById(history.getId());
        if (check.getStatus() != NtHistoryStatus.WAIT.getCode()) {
            throw new ServiceException("状态为【" + NtHistoryStatus.valueOf(check.getStatus()) + "】，不能修改");
        }
        history.setUpdateTime(LocalDateTime.now());
        if (!historyService.updateById(history)) {
            throw new ServiceException("更新失败");
        }
        return new SuccessResponseData();
    }

    /**
     * 删除通知记录
     */
    @ApiOperation("删除通知记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "通知记录编号", required = true, dataType = "String")
    })
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @Log(title = "删除通知记录", businessType = BusinessType.REVOKE)
    @Permission
    public Object delete(String id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException("通知记录编号不能为空");
        }
        History historyOld = historyService.getById(id);
        if (ToolUtil.isEmpty(historyOld)) {
            throw new ServiceException("通知记录不存在，删除失败");
        }
        if (historyOld.getStatus() != NtHistoryStatus.WAIT.getCode()) {
            throw new ServiceException("状态为【" + NtHistoryStatus.valueOf(historyOld.getStatus()) + "】，不能删除");
        }
        if (!historyService.removeById(id)) {
            throw new ServiceException("撤销失败");
        }
        return new SuccessResponseData();
    }

    /**
     * 撤销通知
     */
    @ApiOperation("撤销通知")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "通知记录编号", required = true, dataType = "String")
    })
    @RequestMapping(value = "/revoke", method = RequestMethod.GET)
    @Log(title = "撤销通知", businessType = BusinessType.REVOKE)
    @Permission
    public Object revoke(String id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException("通知记录编号不能为空");
        }
        History historyOld = historyService.getById(id);
        if (ToolUtil.isEmpty(historyOld)) {
            throw new ServiceException("通知记录不存在，撤销失败");
        }

        if (historyOld.getStatus() != NtHistoryStatus.SENT.getCode()) {
            throw new ServiceException("只能撤销已发送的通知记录");
        }
        if (!historyService.revoke(id)) {
            throw new ServiceException("撤销失败");
        }
        return new SuccessResponseData();
    }

    /**
     * 查询通知记录
     */
    @ApiOperation("查询通知记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "通知记录编号", required = true, dataType = "String")
    })
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @Log(title = "查询通知记录", businessType = BusinessType.SELECT_ONE)
    @Permission
    public Object detail(String id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException("通知记录编号不能为空");
        }
        History history = historyService.getById(id);
        if (ToolUtil.isEmpty(history)) {
            throw new ServiceException("通知记录不存在");
        }
        return new SuccessResponseData(history);
    }

    /**
     * 保存并发送通知
     */
    @ApiOperation("保存并发送通知")
    @RequestMapping(value = "/saveSend", method = RequestMethod.POST)
    @Log(title = "保存并发送通知", businessType = BusinessType.OTHER)
    @Permission
    public Object saveSend(History history) throws IOException {
        if (ToolUtil.isNotEmpty(history.getId())) {
            History check = historyService.getById(history.getId());
            if (check.getStatus() != NtHistoryStatus.WAIT.getCode()) {
                throw new ServiceException("状态为【" + NtHistoryStatus.valueOf(check.getStatus()) + "】，不能发送");
            }
        }
        if (ToolUtil.isEmpty(history.getVersion())) {
            throw new ServiceException("版本号不能为空");
        }
        history.setStatus(NtHistoryStatus.SENDING.getCode());
        history.setUpdateTime(LocalDateTime.now());
        if (!historyService.saveOrUpdate(history)) {
            throw new ServiceException("更新失败");
        }
        historyService.send(history);
        return new SuccessResponseData();
    }

    /**
     * 发送通知
     */
    @ApiOperation("发送通知")
    @RequestMapping(value = "/send", method = RequestMethod.GET)
    @Log(title = "发送通知", businessType = BusinessType.OTHER)
    @Permission
    public Object send(String historyId) throws IOException {
        if (ToolUtil.isEmpty(historyId)) {
            throw new ServiceException("发送记录编号不能为空");
        }
        History history = historyService.getById(historyId);
        if (history.getStatus() != NtHistoryStatus.WAIT.getCode()) {
            throw new ServiceException("状态为【" + NtHistoryStatus.valueOf(history.getStatus()) + "】，不能发送");
        }
        history.setStatus(NtHistoryStatus.SENDING.getCode());
        history.setUpdateTime(LocalDateTime.now());
        if (!historyService.updateById(history)) {
            throw new ServiceException("更新失败");
        }
        historyService.send(history);
        return new SuccessResponseData();

    }
}
